<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.polynomial.legendre.legfit &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Legendre Module (numpy.polynomial.legendre)" href="../routines.polynomials.legendre.html" >
    <link rel="next" title="numpy.polynomial.legendre.legvander" href="numpy.polynomial.legendre.legvander.html" >
    <link rel="prev" title="numpy.polynomial.legendre.legfromroots" href="numpy.polynomial.legendre.legfromroots.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.polynomials.html" >Polynomials</a></li>
          <li class="active"><a href="../routines.polynomials.package.html" >Polynomial Package</a></li>
          <li class="active"><a href="../routines.polynomials.legendre.html" accesskey="U">Legendre Module (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.polynomial.legendre</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.polynomial.legendre.legvander.html" title="numpy.polynomial.legendre.legvander"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.polynomial.legendre.legfromroots.html" title="numpy.polynomial.legendre.legfromroots"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.polynomial.legendre.legfromroots.html"
                        title="previous chapter">numpy.polynomial.legendre.legfromroots</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.polynomial.legendre.legvander.html"
                        title="next chapter">numpy.polynomial.legendre.legvander</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-polynomial-legendre-legfit">
<h1>numpy.polynomial.legendre.legfit<a class="headerlink" href="#numpy-polynomial-legendre-legfit" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.polynomial.legendre.legfit">
<code class="sig-prename descclassname">numpy.polynomial.legendre.</code><code class="sig-name descname">legfit</code><span class="sig-paren">(</span><em class="sig-param">x</em>, <em class="sig-param">y</em>, <em class="sig-param">deg</em>, <em class="sig-param">rcond=None</em>, <em class="sig-param">full=False</em>, <em class="sig-param">w=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/polynomial/legendre.py#L1289-L1410"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.polynomial.legendre.legfit" title="Permalink to this definition">¶</a></dt>
<dd><p>Least squares fit of Legendre series to data.</p>
<p>Return the coefficients of a Legendre series of degree <em class="xref py py-obj">deg</em> that is the
least squares fit to the data values <em class="xref py py-obj">y</em> given at points <em class="xref py py-obj">x</em>. If <em class="xref py py-obj">y</em> is
1-D the returned coefficients will also be 1-D. If <em class="xref py py-obj">y</em> is 2-D multiple
fits are done, one for each column of <em class="xref py py-obj">y</em>, and the resulting
coefficients are stored in the corresponding columns of a 2-D return.
The fitted polynomial(s) are in the form</p>
<div class="math">
<p><img src="../../_images/math/9c86e8abeae6b06a2578b6f41e43c1ee889aa8d4.svg" alt="p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),"/></p>
</div><p>where <em class="xref py py-obj">n</em> is <em class="xref py py-obj">deg</em>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>x</strong><span class="classifier">array_like, shape (M,)</span></dt><dd><p>x-coordinates of the M sample points <code class="docutils literal notranslate"><span class="pre">(x[i],</span> <span class="pre">y[i])</span></code>.</p>
</dd>
<dt><strong>y</strong><span class="classifier">array_like, shape (M,) or (M, K)</span></dt><dd><p>y-coordinates of the sample points. Several data sets of sample
points sharing the same x-coordinates can be fitted at once by
passing in a 2D-array that contains one dataset per column.</p>
</dd>
<dt><strong>deg</strong><span class="classifier">int or 1-D array_like</span></dt><dd><p>Degree(s) of the fitting polynomials. If <em class="xref py py-obj">deg</em> is a single integer
all terms up to and including the <em class="xref py py-obj">deg</em>’th term are included in the
fit. For NumPy versions &gt;= 1.11.0 a list of integers specifying the
degrees of the terms to include may be used instead.</p>
</dd>
<dt><strong>rcond</strong><span class="classifier">float, optional</span></dt><dd><p>Relative condition number of the fit. Singular values smaller than
this relative to the largest singular value will be ignored. The
default value is len(x)*eps, where eps is the relative precision of
the float type, about 2e-16 in most cases.</p>
</dd>
<dt><strong>full</strong><span class="classifier">bool, optional</span></dt><dd><p>Switch determining nature of return value. When it is False (the
default) just the coefficients are returned, when True diagnostic
information from the singular value decomposition is also returned.</p>
</dd>
<dt><strong>w</strong><span class="classifier">array_like, shape (<em class="xref py py-obj">M</em>,), optional</span></dt><dd><p>Weights. If not None, the contribution of each point
<code class="docutils literal notranslate"><span class="pre">(x[i],y[i])</span></code> to the fit is weighted by <em class="xref py py-obj">w[i]</em>. Ideally the
weights are chosen so that the errors of the products <code class="docutils literal notranslate"><span class="pre">w[i]*y[i]</span></code>
all have the same variance.  The default value is None.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.5.0.</span></p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl>
<dt><strong>coef</strong><span class="classifier">ndarray, shape (M,) or (M, K)</span></dt><dd><p>Legendre coefficients ordered from low to high. If <em class="xref py py-obj">y</em> was
2-D, the coefficients for the data in column k of <em class="xref py py-obj">y</em> are in
column <em class="xref py py-obj">k</em>. If <em class="xref py py-obj">deg</em> is specified as a list, coefficients for
terms not included in the fit are set equal to zero in the
returned <em class="xref py py-obj">coef</em>.</p>
</dd>
<dt><strong>[residuals, rank, singular_values, rcond]</strong><span class="classifier">list</span></dt><dd><p>These values are only returned if <em class="xref py py-obj">full</em> = True</p>
<p>resid – sum of squared residuals of the least squares fit
rank – the numerical rank of the scaled Vandermonde matrix
sv – singular values of the scaled Vandermonde matrix
rcond – value of <em class="xref py py-obj">rcond</em>.</p>
<p>For more details, see <em class="xref py py-obj">linalg.lstsq</em>.</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Warns</dt>
<dd class="field-odd"><dl>
<dt><strong>RankWarning</strong></dt><dd><p>The rank of the coefficient matrix in the least-squares fit is
deficient. The warning is only raised if <em class="xref py py-obj">full</em> = False.  The
warnings can be turned off by</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">warnings</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s1">&#39;ignore&#39;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">RankWarning</span><span class="p">)</span>
</pre></div>
</div>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><code class="xref py py-obj docutils literal notranslate"><span class="pre">chebfit</span></code>, <code class="xref py py-obj docutils literal notranslate"><span class="pre">polyfit</span></code>, <code class="xref py py-obj docutils literal notranslate"><span class="pre">lagfit</span></code>, <code class="xref py py-obj docutils literal notranslate"><span class="pre">hermfit</span></code>, <code class="xref py py-obj docutils literal notranslate"><span class="pre">hermefit</span></code></p>
<dl class="simple">
<dt><a class="reference internal" href="numpy.polynomial.legendre.legval.html#numpy.polynomial.legendre.legval" title="numpy.polynomial.legendre.legval"><code class="xref py py-obj docutils literal notranslate"><span class="pre">legval</span></code></a></dt><dd><p>Evaluates a Legendre series.</p>
</dd>
<dt><a class="reference internal" href="numpy.polynomial.legendre.legvander.html#numpy.polynomial.legendre.legvander" title="numpy.polynomial.legendre.legvander"><code class="xref py py-obj docutils literal notranslate"><span class="pre">legvander</span></code></a></dt><dd><p>Vandermonde matrix of Legendre series.</p>
</dd>
<dt><a class="reference internal" href="numpy.polynomial.legendre.legweight.html#numpy.polynomial.legendre.legweight" title="numpy.polynomial.legendre.legweight"><code class="xref py py-obj docutils literal notranslate"><span class="pre">legweight</span></code></a></dt><dd><p>Legendre weight function (= 1).</p>
</dd>
<dt><code class="xref py py-obj docutils literal notranslate"><span class="pre">linalg.lstsq</span></code></dt><dd><p>Computes a least-squares fit from the matrix.</p>
</dd>
<dt><a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html#scipy.interpolate.UnivariateSpline" title="(in SciPy v1.4.1)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">scipy.interpolate.UnivariateSpline</span></code></a></dt><dd><p>Computes spline fits.</p>
</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<p>The solution is the coefficients of the Legendre series <em class="xref py py-obj">p</em> that
minimizes the sum of the weighted squared errors</p>
<div class="math">
<p><img src="../../_images/math/561c9a302473d41c77e11d4512d5d9365ee5de22.svg" alt="E = \sum_j w_j^2 * |y_j - p(x_j)|^2,"/></p>
</div><p>where <img class="math" src="../../_images/math/52cb7e0ee86d8a0b7ff7e68a1db495bd058d8198.svg" alt="w_j"/> are the weights. This problem is solved by setting up
as the (typically) overdetermined matrix equation</p>
<div class="math">
<p><img src="../../_images/math/fa1933754de9f1755d11f388c622a82ffbb42e6a.svg" alt="V(x) * c = w * y,"/></p>
</div><p>where <em class="xref py py-obj">V</em> is the weighted pseudo Vandermonde matrix of <em class="xref py py-obj">x</em>, <em class="xref py py-obj">c</em> are the
coefficients to be solved for, <em class="xref py py-obj">w</em> are the weights, and <em class="xref py py-obj">y</em> are the
observed values.  This equation is then solved using the singular value
decomposition of <em class="xref py py-obj">V</em>.</p>
<p>If some of the singular values of <em class="xref py py-obj">V</em> are so small that they are
neglected, then a <em class="xref py py-obj">RankWarning</em> will be issued. This means that the
coefficient values may be poorly determined. Using a lower order fit
will usually get rid of the warning.  The <em class="xref py py-obj">rcond</em> parameter can also be
set to a value smaller than its default, but the resulting fit may be
spurious and have large contributions from roundoff error.</p>
<p>Fits using Legendre series are usually better conditioned than fits
using power series, but much can depend on the distribution of the
sample points and the smoothness of the data. If the quality of the fit
is inadequate splines may be a good alternative.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="rc3400d720244-1"><span class="brackets">1</span></dt>
<dd><p>Wikipedia, “Curve fitting”,
<a class="reference external" href="https://en.wikipedia.org/wiki/Curve_fitting">https://en.wikipedia.org/wiki/Curve_fitting</a></p>
</dd>
</dl>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>